import { Body, Controller, Post, Inject } from '@midwayjs/core';
import { ApiResponse, ApiTags } from '@midwayjs/swagger';
import { Context } from '@midwayjs/koa';
import { ServerResponse } from '../util/response';
import {
  HouseItemResDto,
  Pagination,
  FlagResDto,
  HouseAddDto,
  HouseEditDto,
  HouseIdRequired,
  HouseItemAuditDto,
  HouseItemDetails,
} from '../model';
import { HouseService } from '../service/house.service';

@ApiTags(['房屋'])
@Controller('/house')
export class HouseController {
  @Inject()
  ctx: Context;

  @Inject()
  houseService: HouseService;

  @Post('/list', { summary: '房屋列表' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: HouseItemResDto,
  })
  async list(@Body() req: Pagination) {
    const params: Pagination = {
      pageNo: 1,
      pageSize: 10,
      ...req,
    };
    const res = await this.houseService.getList(params);
    return ServerResponse.JSON_TPL({
      list: res.rows,
      pagination: {
        ...params,
        total: res.count,
      },
    });
  }

  @Post('/listByPerson', { summary: '个人房屋列表' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: HouseItemResDto,
  })
  async listByPerson(@Body() req: Pagination) {
    const params: Pagination = {
      pageNo: 1,
      pageSize: 10,
      ...req,
    };
    const res = await this.houseService.listByPerson(
      params,
      this.ctx.state.user.id
    );
    return ServerResponse.JSON_TPL({
      list: res.rows,
      pagination: {
        ...params,
        total: res.count,
      },
    });
  }

  @Post('/add', { summary: '添加房屋' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: FlagResDto,
  })
  async add(@Body() req: HouseAddDto) {
    const res = await this.houseService.addItem(req, this.ctx.state.user.id);
    return ServerResponse.JSON_TPL({
      flag: Object.keys(res).length > 0,
    });
  }

  @Post('/edit', { summary: '编辑房屋' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: FlagResDto,
  })
  async edit(@Body() req: HouseEditDto) {
    const res = await this.houseService.editItem({
      ...req,
      status:1
    });
    if (Boolean(res[0])) {
      return ServerResponse.JSON_TPL({
        flag: Boolean(res[0]),
      });
    }
    return ServerResponse.JSON_TPL(
      {
        flag: Boolean(res[0]),
      },
      Boolean(res[0]),
      '不存在该房屋'
    );
  }

  @Post('/del', { summary: '删除房屋' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: FlagResDto,
  })
  async del(@Body() req: HouseIdRequired) {
    const res = await this.houseService.delItem(req);
    if (Boolean(res[0])) {
      return ServerResponse.JSON_TPL({
        flag: Boolean(res[0]),
      });
    }
    return ServerResponse.JSON_TPL(
      {
        flag: Boolean(res[0]),
      },
      Boolean(res[0]),
      '不存在该房屋'
    );
  }

  @Post('/byId', { summary: '房屋详情' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type:HouseItemDetails
  })
  async byId(@Body() req: HouseIdRequired) {
    const res = await this.houseService.byIdItem(req)
    return ServerResponse.JSON_TPL({
      data: res,
    });
  }

  @Post('/audit', { summary: '审核房屋' })
  @ApiResponse({
    status: 200,
    description: 'ok',
    type: FlagResDto,
  })
  async audit(@Body() req: HouseItemAuditDto) {
    const res = await this.houseService.auditItem(req)
    if (Boolean(res[0])) {
      return ServerResponse.JSON_TPL({
        flag: Boolean(res[0]),
      });
    }
    return ServerResponse.JSON_TPL(
      {
        flag: Boolean(res[0]),
      },
      Boolean(res[0]),
      '不存在该房屋'
    );
  }
}
